<?php

/**
 * User form.
 *
 * @package    form
 * @subpackage user
 * @version    SVN: $Id: sfPropelFormTemplate.php 6174 2007-11-27 06:22:40Z fabien $
 */
class UserForm extends BaseUserForm
{
  public function configure()
  {
  	if (!$this->getObject()->isNew())
  	{
  	  unset($this['username']);
  	}
  	
  	if (sfContext::getInstance()->getUser()->hasCredential('admin'))
  	{
      unset($this['salt'], 
            $this['password'], 
            $this['remember']
            ); 
  	}
  	else
  	{
      unset($this['salt'], 
            $this['password'], 
            $this['remember'], 
            $this['credential']
            );  		
  	}  	          
  	  	
  	$this->widgetSchema['new_password'] = new sfWidgetFormInputPassword();
  	$this->validatorSchema['new_password'] = new sfValidatorString(array('max_length' => 40, 'required' => false));
  	
  	$this->widgetSchema['repeat'] = new sfWidgetFormInputPassword();
  	$this->validatorSchema['repeat'] = new sfValidatorString(array('max_length' => 40, 'required' => false));
  	
  	$this->widgetSchema->moveField('new_password', sfWidgetFormSchema::AFTER, 'email');
  	$this->widgetSchema->moveField('repeat', sfWidgetFormSchema::AFTER, 'new_password');
  	
  	$this->widgetSchema['credential'] = new sfWidgetFormChoice(array('choices' => sfConfig::get('app_user_credentials')));
    $this->validatorSchema['credential'] = new sfValidatorChoice(array('choices' => sfConfig::get('app_user_credentials')));
  	
  	$this->widgetSchema['file'] = new sfWidgetFormInputFile();        
    $this->validatorSchema['file'] = new sfValidatorFile(array('required' => false));
    
    $this->validatorSchema['url'] = new sfValidatorUrl();
    
  	$this->widgetSchema['visible'] = new sfWidgetFormInputCheckbox(array('value_attribute_value' => '1'));  	
    $this->validatorSchema['visible'] = new sfValidatorBoolean();
    
    $this->validatorSchema->setPostValidator(new sfValidatorPropelUnique(array('model' => 'User', 'column' => 'username')));
      	
  	$this->embedI18n(LanguagePeer::getCulturesArray());
  	
    $decorator = new myWidgetFormSchemaFormatterDl($this->widgetSchema);
    $this->widgetSchema->addFormFormatter('dl', $decorator); 
    $this->widgetSchema->setFormFormatterName('dl');   
  }
  
  public function updateObject($values = null)
  {
  	$old_file = $this->getObject()->getFile();
  	
  	$object = parent::updateObject($values = null);
    
    if ($this->getValue('file'))
    {
      $filename = str_replace(sfConfig::get('sf_upload_dir').DIRECTORY_SEPARATOR, '', $object->getFile());
    
      myThumbnail::thumbnails($filename, $object, $object);
      
      // delete file
      if (file_exists($object->getFile()))
      {
      	unlink($object->getFile());
      }
      
      $object->setFile($filename);
    }
    else
    {
      $object->setFile($old_file);
    }
    
    if ($object->getCredential() === 'visitor')
    {
      $object->setPassword();
    }
    
    if ($this->getValue('new_password'))
    {
      $object->setHashedPassword($this->getValue('new_password'));
    }
  	 
    return $object;
  }
}
